package com.github.baichuan.application.dashboard.controller;

import com.github.baichuan.application.dashboard.dto.auth.ChangePassword;
import com.github.baichuan.application.dashboard.dto.auth.PasswordCredential;
import com.github.baichuan.application.dashboard.feign.AuthService;
import com.github.baichuan.application.dashboard.security.Principal;
import com.github.baichuan.web.dto.Result;
import com.github.baichuan.web.reactive.ClientIP;
import com.github.baichuan.web.security.JwtCodec;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Mono;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.stream.Collectors;

@RestController
@Tag(name = "认证授权")
public class AuthController {

    @Autowired
    private AuthService authService;
    @Autowired
    private JwtCodec<Principal> jwt;

    @PostMapping("/login")
    @Operation(summary = "登录")
    public Mono<?> login(@RequestBody @Valid PasswordCredential credential, @Parameter(hidden = true) ServerHttpRequest request) {
        String ip = ClientIP.get(request);
        credential.setIp(ip);
        return authService.signin(credential).map(result -> {
            if (result.isSuccess()) {

                Map<String, Object> map = (Map) result.getData();
                String memberUid = (String) map.getOrDefault("memberUid", "");
                String memberType = (String) map.getOrDefault("memberType", "");
                String orgBranch = (String) map.getOrDefault("orgBranch", "");

                Principal principal = Principal.builder()
                        .uid(memberUid)
                        .name((String) map.getOrDefault("name", ""))
                        .memberType(memberType)
                        .orgBranch(orgBranch)
                        .authorizations(Arrays.stream(((String) map.getOrDefault("authorizations", "")).split(","))
                                .collect(Collectors.toSet()))
                        .build();


                Map<String, Object> values = new HashMap<>();
                values.put("uid", principal.getUid());
                values.put("name", principal.getName());
                values.put("token", jwt.encode(principal));
                values.put("authorizations", principal.getAuthorizations());

                result.setData(values);
            }

            return result;
        });
    }

    @PostMapping("/password/change")
    @Operation
    public Mono<Result> changePassword(@RequestBody @Valid ChangePassword changePassword, @Parameter(hidden = true) Principal principal) {
        changePassword.setUid(principal.getUid());
        return authService.changePassword(changePassword);
    }
}
